/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.view.PanelCariAnggota;
import com.view.PanelLihatAnggota;
import com.view.PanelLihatPublikasi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import perpus.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author admin
 */
public class Perpustakaan {
    public static String NAMA= "Perpustakaan Kita";

    public TimerBacaPublikasi timerBacaPublikasi;
    public TimerSimpanPublikasi timerSimpanPublikasi;
    
    public static ArrayList <Publikasi> DAFTAR_PUBLIKASI
            = new ArrayList <Publikasi> ();
    
    public static ArrayList <Anggota> DAFTAR_ANGGOTA
            = new ArrayList <Anggota> ();
    
    public static void tambahAnggota (Anggota inAnggota){
        DAFTAR_ANGGOTA.add(inAnggota);
    }
    
    public static void tambahPublikasi (Publikasi inPublikasi){
        DAFTAR_PUBLIKASI.add(inPublikasi);
    }

    public static String bacaDaftarPublikasi (){
        if(DAFTAR_PUBLIKASI.isEmpty()== false){
            String hasil=new String();
            for(int i=0; i<DAFTAR_PUBLIKASI.size(); i++){
                hasil += DAFTAR_PUBLIKASI.get(i).getJudul() + "\n";
            } 
            return hasil;
    } else{
            return "kosong";
    }
}
    
    public static String bacaDaftarAnggota (){
        if(DAFTAR_ANGGOTA.isEmpty()== false){
            String hasil=new String();
            for(int i=0; i<DAFTAR_ANGGOTA.size(); i++){
                hasil += DAFTAR_ANGGOTA.get(i).getNama()+"\n";
            } 
            return hasil;
    } else{
            return "kosong";
    }
    }

    public static void simpanFilePublikasi()  {
        FileOutputStream outStream = null;
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            try {
                File outFile = new File("publikasi.dat");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObjectStream = new ObjectOutputStream(outStream);
                outObjectStream.writeObject(DAFTAR_PUBLIKASI);
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaFilePublikasi() {
        try {
            FileInputStream inStream = null;
            File inFile = new File("publikasi.dat");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObjectStream = new ObjectInputStream(inStream);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void simpanFileAnggota() throws IOException {
        FileOutputStream outStream = null;
        if (DAFTAR_ANGGOTA.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Anggota Kosong");
        } else {
            try {
                File outFile = new File("anggota.dat");
                outStream = new FileOutputStream(outFile);
                String data = new String();
                int i=0;
                if(i<  DAFTAR_ANGGOTA.size()){
                    data += DAFTAR_ANGGOTA.get(i).getNama();
                    i++;
                }
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaFileAnggota() throws IOException, ClassNotFoundException {
            FileInputStream inStream = null;
            File inFile = new File("anggota.dat");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObjectStream = new ObjectInputStream(inStream);
            inObjectStream.readObject();
       }
    
    public static void tambahTabelPublikasi(Publikasi publikasi){
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314063", "MHS115314063");
            Statement statement = conn.createStatement();
            String sql = "insert into publikasi (id_koleksi, judul) values ('"+publikasi.getIdKoleksi() + "','" + publikasi.getJudul()+"')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void bacaTabelPublikasi() throws SQLException, Exception {
        DAFTAR_PUBLIKASI= new ArrayList<Publikasi>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314063", "MHS115314063");
        Statement statement = conn.createStatement();
        String sql = "select * from publikasi";
        ResultSet hasil = statement.executeQuery(sql);
        while (hasil.next()) {
            Publikasi p = new Publikasi() {
                @Override
                public int hitungDenda(int inlamaPinjam) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            };
            p.setIdKoleksi(hasil.getString(1));
            p.setJudul(hasil.getString(2));
            DAFTAR_PUBLIKASI.add(p);
        }
        conn.close();
}
    
    public static void simpanTabelPublikasi(){
        if(DAFTAR_PUBLIKASI.isEmpty()){
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        }else{
            try {
                Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314063", "MHS115314063");
                Statement statement = conn.createStatement();
                int i=0;
                if(i < DAFTAR_PUBLIKASI.size()){
                    
                }
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public static ArrayList<com.model.Anggota> cariAnggota(String nama){
        ArrayList<Anggota> hasil = new ArrayList<Anggota>();
        for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
            Anggota temp = DAFTAR_ANGGOTA.get(i);
            if(temp.getNama().contains(nama)){
                hasil.add(temp);
            }
        }
        return hasil;
    }

    public static void tambahTabelAnggota(Anggota anggota){
        try {
            Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314063", "MHS115314063");
//            Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "HR", "bee");
            Statement statement = conn.createStatement();
            String sql = "insert into anggota (id_anggota, nama) values ('"+anggota.getIdAnggota() +
                    "','" + anggota.getNama()+"')";
            statement.executeUpdate(sql);
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void bacaTabelAnggota() throws SQLException, Exception {
        DAFTAR_ANGGOTA= new ArrayList<Anggota>();
        Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314063", "MHS115314063");
//        Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "HR", "bee");
        Statement statement = conn.createStatement();
        String sql = "select * from anggota";
        ResultSet hasil = statement.executeQuery(sql);
        while (hasil.next()) {
            Anggota a = new Anggota();
            a.setIdAnggota(hasil.getString("id_anggota"));
            a.setNama(hasil.getString("nama"));
            DAFTAR_ANGGOTA.add(a);
        }
        conn.close();
}
    public static void simpanTabelAnggota(){
        if(DAFTAR_ANGGOTA.isEmpty()){
            JOptionPane.showMessageDialog(null, "Daftar Anggota Kosong");
        }else{
            try {
                Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl","MHS115314063", "MHS115314063");
//                Connection conn = OraConnection.open("jdbc:oracle:thin:@localhost:1521:xe", "HR", "bee");
                Statement statement = conn.createStatement();
                int i=0;
                while(i < DAFTAR_ANGGOTA.size()){
                    String sql = "insert into anggota (id_anggota, nama) values ('"+DAFTAR_ANGGOTA.get(i).getIdAnggota()
                            + "','" + DAFTAR_ANGGOTA.get(i).getNama()+"')";
                    statement.executeUpdate(sql);
                    i++;
                }
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void panelTabelPublikasi() {
        JFrame test = new JFrame();
        PanelLihatPublikasi ptk = new PanelLihatPublikasi(DAFTAR_PUBLIKASI);
        test.getContentPane().add(ptk);
        test.setSize(800, 250);
        test.setLocation(250, 200);
        test.setVisible(true);
        test.setTitle("Panel Lihat Publikasi");
    }

    public static void panelTabelAnggota() {
        JFrame test = new JFrame();
        PanelLihatAnggota ptk = new PanelLihatAnggota(DAFTAR_ANGGOTA);
        test.getContentPane().add(ptk);
        test.setSize(800, 250);
        test.setLocation(500, 150);
        test.setVisible(true);
        test.setTitle("Panel Lihat Anggota");
    }

     public static void panelTabelCariAnggota() {
        JFrame test = new JFrame();
        PanelCariAnggota ptk = new PanelCariAnggota(DAFTAR_ANGGOTA);
        test.getContentPane().add(ptk);
        test.setSize(350, 500);
        test.setLocation(250, 200);
        test.setVisible(true);
        test.setTitle("Panel Lihat Anggota");
    }
}